<template>
  <view class="w-full max-w-[750rpx] mx-auto min-h-screen bg-[#f6f8fa] p-[32rpx] box-border">
    <backView />
    <text class="block text-[48rpx] font-extrabold mb-[40rpx] text-center text-blue-600 drop-shadow">查看周期性健康检查记录</text>
    <FormView :fieldConfig="fieldConfig" :form="form" :showButtons="false" />
    
    <!-- 操作按钮 -->
    <view class="mt-[48rpx] flex gap-[24rpx]">
      <nut-button block type="default" class="h-[96rpx] rounded-[16rpx] text-[32rpx] font-bold shadow flex-1" @click="goBack">返回</nut-button>
      <nut-button block type="primary" class="h-[96rpx] rounded-[16rpx] text-[32rpx] font-bold shadow flex-1 bg-blue-500 hover:bg-blue-600 active:bg-blue-700" @click="goEdit">编辑</nut-button>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import Taro from '@tarojs/taro'
import { healthExamRecordFields as fieldConfig } from './fields'
import FormView from '@/components/FormView.vue'
import { request } from '@/service/request'
import backView from '@/components/backView.vue'

const form = reactive<any>({})
const recordId = ref('')

onMounted(async () => {
  console.log('查看周期性健康检查记录页面加载')
  const params = Taro.getCurrentInstance().router?.params
  if (params?.id) {
    recordId.value = params.id
    await loadRecord(params.id)
  }
})

async function loadRecord(id: string) {
  try {
    const result = await request.get<any>(`/health_records/health_exam/healthExamIndexInfo/queryById?id=${id}`, {}, {
      useErrMsg: true
    })
    
    if (result) {
      Object.assign(form, result)
    }
  } catch (error) {
    console.error('加载记录失败:', error)
    Taro.showToast({ title: '加载记录失败', icon: 'error' })
  }
}

function goBack() {
  Taro.navigateBack()
}

function goEdit() {
  Taro.navigateTo({
    url: `/pages/historys/health-exam-record/edit?id=${recordId.value}`
  })
}
</script>
